home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / wdj0696.zip / ELLIOTT.ZIP / TRACEX.CPP next >
C/C++ Source or Header  |  1996-01-17  |  6KB  |  218 lines

  1. // =========================================================
  2. // tracex implementation
  3. // =========================================================
  4. #include "stdafx.h"        // possibly needed for PCH
  5. #include "tracex.h"
  6.  
  7. // static members
  8. // --------------
  9. int  CTracex::mIndent = 0;
  10. BOOL CTracex::mTracing = TRUE;
  11.  
  12. // =========================================================
  13. // CTracex constructor
  14. // =========================================================
  15. CTracex::CTracex(CString pMsg)
  16.  {// Remember the message string
  17.   // ---------------------------
  18.   mMsg = pMsg;
  19.  
  20.   // If we are tracing, display a trace
  21.   // ----------------------------------
  22.   if (mTracing == TRUE)
  23.    {traceIndent();
  24.     TRACE(pMsg + "\n");
  25.    }
  26.  
  27.   // Move the indentation level up a notch
  28.   // -------------------------------------
  29.   mIndent += 2;
  30.  }
  31.  
  32. // =========================================================
  33. // CTracex destructor
  34. // =========================================================
  35. CTracex::~CTracex()
  36.  {ASSERT_VALID(this);
  37.  
  38.   // Move the indentation level back down a notch
  39.   // --------------------------------------------
  40.   mIndent -= 2;
  41.   
  42.   // If we are tracing, display the trace exit message
  43.   // -------------------------------------------------
  44.   if (mTracing == TRUE)
  45.    {traceIndent();
  46.     mMsg += " exit\n";
  47.     TRACE(mMsg);
  48.    }
  49.  }
  50.  
  51. // =========================================================
  52. // AssertValid
  53. // Inherited from CObject.
  54. // =========================================================
  55. void CTracex::AssertValid() const
  56.  {CObject::AssertValid();
  57.  }
  58.  
  59. // =========================================================
  60. // traceIndent
  61. // Used privately to display an indentation
  62. // =========================================================
  63. void CTracex::traceIndent()
  64.  {ASSERT_VALID(this);
  65.  
  66.   if (mTracing == TRUE)
  67.    {if (mIndent > 0)
  68.  
  69.      // crude but effective
  70.      // -------------------
  71.      {for (int tCount = 0; tCount < mIndent; tCount++)
  72.        TRACE0(" ");
  73.      }
  74.    }
  75.  }
  76.  
  77. // =========================================================
  78. // msg
  79. // Use this to display a message at the current indentation
  80. // level
  81. // =========================================================
  82. void CTracex::msg(const CString pMsg)
  83.  {ASSERT_VALID(this);
  84.  
  85.   if (mTracing == TRUE)  
  86.    {traceIndent();
  87.     TRACE(mMsg);
  88.     TRACE0(" : ");  
  89.     TRACE((pMsg.GetLength() > 511) ? pMsg.Left(511) : pMsg);
  90.  
  91.     TRACE0("\n");
  92.    }
  93.  }
  94.  
  95. // =========================================================
  96. // msg (overloaded)
  97. // Displays a CString and an int
  98. // =========================================================
  99. void CTracex::msg(const CString pMsg, const int pInt)
  100.  {ASSERT_VALID(this);
  101.   
  102.   char tStr[10]={0};
  103.   itoa(pInt, tStr, 10);
  104.   msg(pMsg + (CString)tStr);
  105.  }
  106.  
  107. // =========================================================
  108. // msg (overloaded)
  109. // Displays a CString and an unsigned int
  110. // =========================================================
  111. void CTracex::msg(const CString pMsg, 
  112.                   const unsigned int pUint)
  113.  {ASSERT_VALID(this);
  114.   msg(pMsg, (unsigned long)pUint);
  115.  }
  116.  
  117. // =========================================================
  118. // msg (overloaded)
  119. // Displays a CString and a long
  120. // =========================================================
  121. void CTracex::msg(const CString pMsg, const long pLong)
  122.  {ASSERT_VALID(this);
  123.   
  124.   char tStr[30]={0};
  125.   ltoa(pLong, tStr, 10);
  126.   msg(pMsg + (CString)tStr);
  127.  }
  128.  
  129. // =========================================================
  130. // msg (overloaded)
  131. // Displays a CString and an unsigned long
  132. // =========================================================
  133. void CTracex::msg(const CString pMsg, 
  134.                   const unsigned long pUlong)
  135.  {ASSERT_VALID(this);
  136.   
  137.   char tStr[30]={0};
  138.   ultoa(pUlong, tStr, 10);
  139.   msg(pMsg + (CString)tStr);
  140.  }
  141.  
  142. // =========================================================
  143. // msg (overloaded)
  144. // Displays a CString and a double
  145. // NOTE: I have arbitrarilly chosen to display only seven
  146. // digits to the right of the decimal.
  147. // =========================================================
  148. void CTracex::msg(const CString pMsg, const double pDouble)
  149.  {ASSERT_VALID(this);
  150.   
  151.   int decimal, sign;
  152.   char* tStr;
  153.   tStr = _fcvt(pDouble, 7, &decimal, &sign);
  154.  
  155.   // _fcvt doesn't do the formatting of the sign and the 
  156.   // decimal so we must do that here.
  157.   // ---------------------------------------------------
  158.   CString tString = "";
  159.   const CString tDot = ".";
  160.  
  161.   if (sign != 0)
  162.    tString = (CString)"-";
  163.  
  164.   if (decimal < 1)
  165.    {tString += tDot;
  166.  
  167.     for (int tSub = 0; tSub > decimal; tSub--)
  168.      tString += (CString)"0";
  169.  
  170.     tString += (CString)tStr;
  171.    }
  172.  
  173.   else if (decimal >= (int)strlen(tStr))
  174.    tString += (CString)tStr + tDot;
  175.  
  176.   else
  177.    {for (int tSub = 0; tSub < decimal; tSub++)
  178.      tString += tStr[tSub];
  179.  
  180.     tString += tDot;
  181.  
  182.     for (tSub = decimal; tSub < (int)strlen(tStr); tSub++)
  183.      tString += tStr[tSub];
  184.    }
  185.  
  186.   msg(pMsg + tString);
  187.  }
  188.  
  189. // =========================================================
  190. // msg (overloaded)
  191. // Displays a CString and another CString
  192. // =========================================================
  193.  void CTracex::msg(const CString pMsg, 
  194.                    const CString pString)
  195.   {ASSERT_VALID(this);
  196.   
  197.    msg(pMsg + pString);
  198.   }
  199.  
  200. // =========================================================
  201. // msg (overloaded)
  202. // Displays a CString and calls the CObject*'s Dump() 
  203. // member.
  204. // =========================================================
  205.  void CTracex::msg(const CString pMsg,
  206.                    const CObject* pObject)
  207.   {ASSERT_VALID(this);
  208.   
  209.    CString tSep = "==========";
  210.    msg(tSep + " " + pMsg + " dump " + tSep);
  211.  
  212.    #ifdef _DEBUG
  213.    pObject->Dump(afxDump);
  214.    #endif
  215.  
  216.    msg(tSep + " end of " + pMsg + " dump " + tSep);
  217.   }
  218.